package com.study.testSpringBoot.scheduled;

import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.study.testSpringBoot.concurrent.DataModel;
import com.study.testSpringBoot.concurrent.DataModelAtomic;
import com.study.testSpringBoot.concurrent.DataModelLock;
import com.study.testSpringBoot.service.impl.ProviderServiceImpl;

@Component
public class ComsumerTask {

	@Autowired
	private DataModel dataModel;
	
	@Autowired
	private DataModelLock dataModelLock;
	
	@Autowired
	private DataModelAtomic dataModelAtomic;
	
	@Autowired
	private ProviderServiceImpl providerService;
	
	private AtomicInteger result = new AtomicInteger(0);
	
	@Scheduled(fixedRate = 3000)
	public void comsumer() {
		Map<String, Integer> data = dataModel.getAndDelData();
//		Map<String, Integer> data = dataModelLock.getAndDelData();
//		Map<String, Integer> data = dataModelAtomic.getAndDelData();
		if(!data.isEmpty()) {
			int num = data.get("ceshi");
			result.compareAndSet(result.get(), num+result.get());
			
			System.out.println("-----生产数据："+providerService.getResult().get());
			System.out.println("-----消费数据："+result);
		}
		
	}
}
